package com.sl.biz.modular.cdc.dao.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.biz.modular.cdc.dao.domain.entity.CdcConfigDO;
import com.sl.biz.modular.cdc.dao.enums.CdcStatusEnum;
import com.sl.biz.modular.cdc.dao.mapper.CdcConfigMapper;
import com.sl.biz.modular.cdc.dao.service.CdcConfigDaoService;
import com.sl.common.enums.CommonDsiableFlagEnum;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CdcConfigDaoServiceImpl extends ServiceImpl<CdcConfigMapper, CdcConfigDO> implements CdcConfigDaoService {
    @Override
    public List<CdcConfigDO> queryRunnerCdcConfig() {
        LambdaQueryWrapper<CdcConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CdcConfigDO::getDisableFlag, CommonDsiableFlagEnum.ENABLE_FLAG.getCode());
        lambdaQueryWrapper.eq(CdcConfigDO::getStatus, CdcStatusEnum.RUN.getCode());
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public List<CdcConfigDO> listByCondition(CdcConfigDO jobConfigDO) {
        LambdaQueryWrapper<CdcConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotNull(jobConfigDO.getDisableFlag()), CdcConfigDO::getDisableFlag, jobConfigDO.getDisableFlag());
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(jobConfigDO.getStatus()), CdcConfigDO::getStatus, jobConfigDO.getStatus());
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(jobConfigDO.getGroupId()), CdcConfigDO::getGroupId, jobConfigDO.getGroupId());

        return super.list(lambdaQueryWrapper);
    }

    @Override
    public List<CdcConfigDO> getByIds(List<String> cdcConfigId) {
        LambdaQueryWrapper<CdcConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CdcConfigDO::getId, cdcConfigId);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public Boolean updateStatus(CdcConfigDO cdcConfigDO) {
        LambdaUpdateWrapper<CdcConfigDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(CdcConfigDO::getStatus, cdcConfigDO.getStatus());
        lambdaUpdateWrapper.eq(CdcConfigDO::getId, cdcConfigDO.getId());
        return super.update(lambdaUpdateWrapper);
    }

    @Override
    public Boolean updateDisableFlag(CdcConfigDO assertUserUse) {
        LambdaUpdateWrapper<CdcConfigDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(CdcConfigDO::getDisableFlag, assertUserUse.getDisableFlag());
        lambdaUpdateWrapper.eq(CdcConfigDO::getId, assertUserUse.getId());
        return super.update(lambdaUpdateWrapper);
    }
}
